﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StoragePeriod.IDAL.FoodTesting;
using StoragePeriod.Model.FoodTesting;
using System.Data;
using StoragePeriod.DBUtility;
using System.Data.SqlClient;

namespace StoragePeriod.SqlServerDAL.FoodTesting
{
    /// <summary>
    /// 碳水化合物 接口
    /// </summary>
    public class D_CarbohydratesData : I_CarbohydratesData
    {
        /// <summary>
        /// 获取Vc含量检查实体
        /// </summary>
        /// <param name="eVcData">查询ID</param>
        /// <returns>返回对应的实体</returns>
        public E_CarbohydratesData GetDataEntityById(E_CarbohydratesData eCarbohydratesData)
        {
            StringBuilder sbSql = new StringBuilder();
            sbSql.Append("select * from CarbohydratesData where id=" + eCarbohydratesData.id);
            DataTable dt = DbHelperSQL.Query(sbSql.ToString()).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                E_CarbohydratesData Entity = new ModelHandler<E_CarbohydratesData>().FillModel(dt.Rows[0]);
                return Entity;
            }
            return null;
        }

        /// <summary>
        /// 根据原料ID和检测时间 删除检测记录
        /// </summary>
        /// <param name="eVcData">要删除的检测记录</param>
        /// <returns></returns>
        public int RemoveByTime(E_CarbohydratesData eCarbohydratesData) 
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CarbohydratesData where RawId=" + eCarbohydratesData.RawId + " and CarbohydratesTime=CAST('" + eCarbohydratesData.CarbohydratesTime.ToString() + "' as datetime)");
            return DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 删除某一次检测记录
        /// </summary>
        /// <param name="eVcData">要删除的检测记录</param>
        /// <returns></returns>
        public int RemoveByFoodNum(E_CarbohydratesData eCarbohydratesData) 
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CarbohydratesData where RawId=" + eCarbohydratesData.RawId + " and CarbohydratesTime=CAST('" + eCarbohydratesData.CarbohydratesTime.ToString() + "' as datetime) and FoodNum=" + eCarbohydratesData.FoodNum);
            return DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 根据原料ID 和检测时间 获取所有的检测记录
        /// </summary>
        /// <param name="eVcData">要获取的检测记录</param>
        /// <returns></returns>
        public List<E_CarbohydratesData> GetDataListByTime(E_CarbohydratesData eCarbohydratesData) 
        {
            StringBuilder sbSql = new StringBuilder();
            sbSql.Append("select * from CarbohydratesData where RawId=" + eCarbohydratesData.RawId + " and CarbohydratesTime=cast('" + eCarbohydratesData.CarbohydratesTime.ToString() + "' as datetime)");
            DataTable dt = DbHelperSQL.Query(sbSql.ToString()).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                List<E_CarbohydratesData> EntityList = new ModelHandler<E_CarbohydratesData>().FillModel(dt);
                return EntityList;
            }
            return null;
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(E_CarbohydratesData eCarbohydratesData) 
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into CarbohydratesData(");
            strSql.Append("RawId,RawName,Carbohydrates_XGDJZ,Carbohydrates_X,Carbohydrates_m1,Carbohydrates_m,Carbohydrates_v,Carbohydrates_ZT,Carbohydrates_F,Carbohydrates_V1,Carbohydrates_V2,CarbohydratesTime,FoodNum)");
            strSql.Append(" values (");
            strSql.Append("@RawId,@RawName,@Carbohydrates_XGDJZ,@Carbohydrates_X,@Carbohydrates_m1,@Carbohydrates_m,@Carbohydrates_v,@Carbohydrates_ZT,@Carbohydrates_F,@Carbohydrates_V1,@Carbohydrates_V2,@CarbohydratesTime,@FoodNum)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@RawId", SqlDbType.Int,4),
					new SqlParameter("@RawName", SqlDbType.VarChar,50),
					new SqlParameter("@Carbohydrates_XGDJZ", SqlDbType.VarChar,50),
					new SqlParameter("@Carbohydrates_X", SqlDbType.VarChar,50),
					new SqlParameter("@Carbohydrates_m1", SqlDbType.VarChar,50),
					new SqlParameter("@Carbohydrates_m", SqlDbType.VarChar,50),
					new SqlParameter("@Carbohydrates_v", SqlDbType.VarChar,50),
					new SqlParameter("@Carbohydrates_ZT", SqlDbType.VarChar,50),
					new SqlParameter("@Carbohydrates_F", SqlDbType.VarChar,50),
					new SqlParameter("@Carbohydrates_V1", SqlDbType.VarChar,50),
					new SqlParameter("@Carbohydrates_V2", SqlDbType.VarChar,50),
					new SqlParameter("@CarbohydratesTime", SqlDbType.DateTime),
					new SqlParameter("@FoodNum", SqlDbType.Int,4)};
            parameters[0].Value = eCarbohydratesData.RawId;
            parameters[1].Value = eCarbohydratesData.RawName;
            parameters[2].Value = eCarbohydratesData.Carbohydrates_XGDJZ;
            parameters[3].Value = eCarbohydratesData.Carbohydrates_X;
            parameters[4].Value = eCarbohydratesData.Carbohydrates_m1;
            parameters[5].Value = eCarbohydratesData.Carbohydrates_m;
            parameters[6].Value = eCarbohydratesData.Carbohydrates_v;
            parameters[7].Value = eCarbohydratesData.Carbohydrates_ZT;
            parameters[8].Value = eCarbohydratesData.Carbohydrates_F;
            parameters[9].Value = eCarbohydratesData.Carbohydrates_V1;
            parameters[10].Value = eCarbohydratesData.Carbohydrates_V2;
            parameters[11].Value = eCarbohydratesData.CarbohydratesTime;
            parameters[12].Value = eCarbohydratesData.FoodNum;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
    }
}
